﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ABCTile.Models;
using ABCTile.DAL;
using ABCTile.ViewModel;
using PagedList;

namespace ABCTile.Controllers
{ 
    public class TileController : Controller
    {
        private ABCDbContext db = new ABCDbContext();

        //
        // GET: /Tile/
        [Authorize]
        public ViewResult Index(int? page, TileIndexData tileIndexData)
        {
            /*
             * "TileIndexData" is defined in ABCTile/ViewModel. 
             * "TileIndexData" has 
             *   public IEnumerable<Tile> Tiles { get; set; } // one or more tile can be saved in here
             *   public IPagedList<Tile> PagedTiles { get; set; } // one or more tile can be saved in here
             *   public string ColorID { get; set; } // For the dropdown list menu that can be used for searching tile
             *   public int ShapeID { get; set; } // For the dropdown list menu that can be used for searching tile
             *   public int ManufactureID { get; set; } // For the dropdown list menu that can be used for searching tile
             * */
            var ViewModel = new TileIndexData();

            //Get all tile data from Database and save them ViewModel.Tiles
            ViewModel.Tiles = db.Tiles.Include(t => t.Color).Include(t => t.Shape).Include(t => t.Mannufacture).ToList();

            /*Compare colorID that is derieved from dropdown list and ColorID in viewModel.Tiles */
            if (tileIndexData.ColorID != null)
            {
                ViewModel.Tiles = ViewModel.Tiles.Where(t => t.ColorID.Equals(tileIndexData.ColorID));
            }

            /*Compare ShapeID that is derieved from dropdown list and ShapeID in viewModel.Tiles */
            if (tileIndexData.ShapeID != 0)
            {
                ViewModel.Tiles = ViewModel.Tiles.Where(t => t.ShapeID == tileIndexData.ShapeID);
            }

            /*Compare ManufactureID that is derieved from dropdown list and ManufactureID in viewModel.Tiles */
            if (tileIndexData.ManufactureID != 0 )
            {
                ViewModel.Tiles = ViewModel.Tiles.Where(t => t.ManufactureID == tileIndexData.ManufactureID);
            }

            /*Set page number as 1 if request is get*/
            if (Request.HttpMethod != "GET")
            {
                page = 1;
            }

            int pageSize = 10; //set page size, 10 tile data will be displayed on the screen
            int pageIndex = (page ?? 1) - 0; //set page index

            ViewModel.PagedTiles = ViewModel.Tiles.ToPagedList(pageIndex, pageSize); //Convert "viewModel.tiles" into "PagedList" Type to diplay 10 tile data on a page

            /*Get all color, shape, manufacture data to display them in dropdown list menu*/
            ViewBag.ColorID = new SelectList(db.Colors.ToList(), "ColorID", "ColorName");
            ViewBag.ShapeID = new SelectList(db.Shapes.ToList(), "ShapeID", "ShapeName");
            ViewBag.ManufactureID = new SelectList(db.Manufactures, "ManufactureID", "Name");

            return View(ViewModel);
        }

        //
        // GET: /Tile/Details/5
        [Authorize]
        public ViewResult Details(int id)
        {
            Tile tile = db.Tiles.Find(id);
            return View(tile);
        }

        //
        // GET: /Tile/Create
        [Authorize]
        public ViewResult Create()
        {
            ViewBag.ColorID = new SelectList(db.Colors, "ColorID", "ColorName");
            ViewBag.ShapeID = new SelectList(db.Shapes, "ShapeID", "ShapeName");
            ViewBag.ManufactureID = new SelectList(db.Manufactures, "ManufactureID", "Name");
            return View();
        } 

        //
        // POST: /Tile/Create

        [HttpPost]
        public ActionResult Create(Tile tile)
        {
            if (ModelState.IsValid)
            {
                db.Tiles.Add(tile);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ColorID = new SelectList(db.Colors, "ColorID", "ColorName", tile.ColorID);
            ViewBag.ShapeID = new SelectList(db.Shapes, "ShapeID", "ShapeName", tile.ShapeID);
            ViewBag.ManufactureID = new SelectList(db.Manufactures, "ManufactureID", "Name", tile.ManufactureID);
            return View(tile);
        }
        
        //
        // GET: /Tile/Edit/5
        [Authorize]
        public ViewResult Edit(int id)
        {
            Tile tile = db.Tiles.Find(id);
            ViewBag.ColorID = new SelectList(db.Colors, "ColorID", "ColorName", tile.ColorID);
            ViewBag.ShapeID = new SelectList(db.Shapes, "ShapeID", "ShapeName", tile.ShapeID);
            ViewBag.ManufactureID = new SelectList(db.Manufactures, "ManufactureID", "Name", tile.ManufactureID);
            return View(tile);
        }

        //
        // POST: /Tile/Edit/5

        [HttpPost]
        public ActionResult Edit(Tile tile)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tile).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ColorID = new SelectList(db.Colors, "ColorID", "ColorName", tile.ColorID);
            ViewBag.ShapeID = new SelectList(db.Shapes, "ShapeID", "ShapeName", tile.ShapeID);
            ViewBag.ManufactureID = new SelectList(db.Manufactures, "ManufactureID", "Name", tile.ManufactureID);
            return View(tile);
        }

        //
        // GET: /Tile/Delete/5
        [Authorize]
        public ViewResult Delete(int id)
        {
            Tile tile = db.Tiles.Find(id);
            return View(tile);
        }

        //
        // POST: /Tile/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Tile tile = db.Tiles.Find(id);
            db.Tiles.Remove(tile);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}